//
//  ChateBottomView.swift
//  ModernFoodWebsite
//
//  Created by apple on 16/6/1.
//  Copyright © 2016年 Lonnie. All rights reserved.
//

import UIKit

class ChatBottomView: UIView {
    var textField:UITextField = UITextField()
    var imageButton = UIButton()
    var viewWillShowObserver:NSObjectProtocol?
    var viewWillHideObserver:NSObjectProtocol?
    var bottomConstraint:NSLayoutConstraint?
    var text:String = ""
    var shouldSendImage = true
    var willSendImageHandler:()->Void = {}
    var willSendTexthandler:(String)->Void = {text in
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        textField.backgroundColor = UIColor(red: 0x99/255.0, green: 0x99/255.0, blue: 0x99/255.0, alpha: 1.0)
        textField.layer.cornerRadius = 4
        textField.layer.masksToBounds = true
        textField.attributedPlaceholder = NSAttributedString(string: "请输入您要发送的内容", attributes: [NSForegroundColorAttributeName:UIColor(red: 0x66/255.0, green: 0x66/255.0, blue: 0x66/255.0, alpha: 1.0)])
       
        imageButton.setImage( UIImage(fromColor: UIColor.redColor(), rect: CGRectMake(0, 0, 30, 30)), forState: .Normal)
        addSubviews([textField,imageButton])
        textField.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.leading.equalTo(self).offset(15)
            make.trailing.equalTo(self).offset(-65)
            make.height.equalTo(36)
        }
        textField.addTarget(self, action: #selector(ChatBottomView.changeText(_:)), forControlEvents: .AllEditingEvents)
        imageButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(self).offset(-12.5)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSizeMake(30, 30))
        }
        imageButton.addTapAction { (tap) in
            self.endEditing(true)
            if self.text.characters.count == 0 {
                self.willSendImageHandler()
            } else {
                self.willSendTexthandler(self.text)
                self.text = ""
                self.textField.text = ""
            }
        }
    }
    
    func changeText(tf:UITextField) {
        self.text = tf.text!
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showInView(view:UIView) {
        self.viewWillShowObserver = NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillShowNotification, object: nil, queue: nil, usingBlock: { (note) in
            print("\(note.userInfo)")
            view.layoutIfNeeded()
            
            self.imageButton.setImage( UIImage(fromColor: UIColor.greenColor(), rect: CGRectMake(0, 0, 30, 30)), forState: .Normal)
            let frame = (note.userInfo!["UIKeyboardFrameBeginUserInfoKey"] as! NSValue).CGRectValue()
            view.layoutIfNeeded()
            UIView.animateWithDuration(0.3, animations: {
                self.bottomConstraint?.constant = -CGRectGetHeight(frame)
            })
        })
        self.viewWillHideObserver = NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillHideNotification, object: nil, queue: nil, usingBlock: { (note) in
            print("\(note.userInfo)")

            self.imageButton.setImage( UIImage(fromColor: UIColor.redColor(), rect: CGRectMake(0, 0, 30, 30)), forState: .Normal)
            view.layoutIfNeeded()
            UIView.animateWithDuration(0.3, animations: { 
                self.bottomConstraint?.constant = 0
            })
            
 
        })
        view.addSubview(self)
        self.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(view)
            make.height.equalTo(50)
        }
        view.constraints.forEach { (c) in
 
            if c.firstItem.hashValue  == self.hashValue   || c.firstItem.hashValue  ==  self.hashValue  {
                if c.firstAttribute == .Bottom || c.secondAttribute == .Bottom {
                    self.bottomConstraint = c
                }
            }
        }
    }
    
    func dismiss() {
        NSNotificationCenter.defaultCenter().removeObserver(self.viewWillShowObserver!)
        NSNotificationCenter.defaultCenter().removeObserver(self.viewWillHideObserver!)
        removeFromSuperview()
    }
}
